package com.swy.shopgroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.swy.shopgroup.adapter.GoInAdapter;
import com.swy.shopgroup.adapter.PageOutAdapter;
import com.swy.shopgroup.entity.OutPage;
import com.swy.shopgroup.entity.ResultBean;
import com.swy.shopgroup.entity.ShopGoIn;
import com.swy.shopgroup.network.NetAdress;
import com.swy.shopgroup.network.OkHttp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Response;

public class MainActivity3 extends AppCompatActivity {

    private SwipeRefreshLayout swipe;
    private List<ShopGoIn.DataBean> list = new ArrayList<>();
    private GoInAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        Utils.checkAllPer(this);
        initView();
        initData();
    }


    public void initView() {
        swipe = findViewById(R.id.swipe);
        mAdapter = new GoInAdapter(this, list);
        ListView listView = findViewById(R.id.listView);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder alterDiaglog = new AlertDialog.Builder(MainActivity3.this, R.style.MyDialog);
                View vv = LayoutInflater.from(MainActivity3.this).inflate(R.layout.alert_go_in, null, false);
                alterDiaglog.setView(vv);
                final AlertDialog dialog = alterDiaglog.create();
                Button commitBtn = vv.findViewById(R.id.commitBtn);
                Button cancelBtn = vv.findViewById(R.id.cancel_button);
                commitBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        updateShop(list.get(position).getId() + "");
                        dialog.dismiss();
                    }
                });
                cancelBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
    }

    public void initData() {
        selectShop();
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                selectShop();
            }
        });
    }

    @SuppressLint("CheckResult")
    public void selectShop() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                OkHttp o = new OkHttp();
                try {
                    Response response = o.getDataWhitId(NetAdress.shopState, "0");
                    e.onNext(response.body().string());
                } catch (IOException a) {
                    a.printStackTrace();
                    e.onNext("s");
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new io.reactivex.functions.Consumer<String>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull String s) throws Exception {
                try {
                    Gson gson = new Gson();
                    ShopGoIn result = gson.fromJson(s, ShopGoIn.class);
                    if (result.getCode().equals("200")) {
                        list.clear();
                        list.addAll(result.getData());
                        mAdapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {

                }
                swipe.setRefreshing(false);
            }
        });
    }

    @SuppressLint("CheckResult")
    public void updateShop(final String id) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                OkHttp o = new OkHttp();
                try {

                    Response response = o.getDataWhitId(NetAdress.shopUpdate, id);
                    e.onNext(response.body().string());
                } catch (IOException a) {
                    a.printStackTrace();
                    e.onNext("s");
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new io.reactivex.functions.Consumer<String>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull String s) throws Exception {
                try {
                    Gson gson = new Gson();
                    ResultBean result = gson.fromJson(s, ResultBean.class);
                    if (result.getCode().equals("200")) {
                        selectShop();
                    }
                    Toast.makeText(MainActivity3.this, result.getMsg(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {

                }
                swipe.setRefreshing(false);
            }
        });
    }
}